﻿/*  CamHelper (See http://camhelper.codeplex.com/ or https://bitbucket.org/AndiKS/camhelper for more Info)
    Copyright (C) 2011 Andreas Wimmer

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Diagnostics;
using CamHelper.Interpreter;

namespace CamHelper.Command
{
    public class ParserTestCommand : IOfflineCommand
    {
        public void Run()
        {
            while (true)
            {
                Console.Write("Command: ");
                var cmd = Console.ReadLine();

                if (cmd == "exit")
                    break;
                else if (cmd.Length == 0)
                    continue;

                var parser = new Parser();
                parser.Parsing += new EventHandler<ParseEventArgs>(parser_Parsing);
                parser.Parsed += new EventHandler<ParseEventArgs>(parser_Parsed);
                parser.Calculating += new EventHandler<ParseEventArgs>(parser_Calculating);
                parser.Calculated += new EventHandler<ParseEventArgs>(parser_Calculated);

                var ps = parser.SearchParameter(cmd);
                foreach (var item in ps)
                {
                    Console.Write("Value for Parameter {0}: ", item.Key);
                    item.Value.Value = Console.ReadLine();
                }
                parser.Parameter = ps;

                var result = parser.Calc(cmd);
                Console.WriteLine("Result: {0}{1}", result, Environment.NewLine);
            }
        }

        void parser_Parsing(object sender, ParseEventArgs e)
        {
            Debug.WriteLine(new String('-', 30));

            var s = String.Format("Start Parsing: {0}('{1}')-{2}('{3}')", e.Statement.StartToken, e.Parser.Tokens[e.Statement.StartToken], e.Statement.EndToken, e.Parser.Tokens[e.Statement.EndToken]);
            Debug.WriteLine(s);
        }

        void parser_Parsed(object sender, ParseEventArgs e)
        {
            var s = String.Format("Parsed: {0}('{1}')-{2}('{3}')", e.Statement.StartToken, e.Parser.Tokens[e.Statement.StartToken], e.Statement.EndToken, e.Parser.Tokens[e.Statement.EndToken]);
            Debug.WriteLine(s);
            Debug.WriteLine(e.Statement);

            Debug.WriteLine(new String('-', 30));
            Debug.WriteLine(String.Empty);
        }

        void parser_Calculating(object sender, ParseEventArgs e)
        {
            Debug.WriteLine(new String('-', 30));

            var s = String.Format("Start Calculating: {0}", e.Statement.Command);
            Debug.WriteLine(s);
        }

        void parser_Calculated(object sender, ParseEventArgs e)
        {
            var s = String.Format("Calculated: {0}{1}Result: {2}", e.Statement.CommandAfterCalculatedChild, Environment.NewLine, e.Statement.Result);
            Debug.WriteLine(s);

            Debug.WriteLine(new String('-', 30));
            Debug.WriteLine(String.Empty);
        }
    }
}
